/**
 * QIBAO Service Inc
 * All Rights Reserved @2019
 */
package com.qibao.toolkit.web;

import com.qibao.toolkit.util.AppVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>版本控制器。</p>
 *
 * @author zhanghui
 * @version $Id: VersionController.java, v0.1 2019/9/10 11:48 zhanghui Exp $$
 */
@RestController
public class VersionController implements ApplicationListener<ApplicationPreparedEvent> {

    private static final Logger logger = LoggerFactory.getLogger(VersionController.class);

    private static Class<?> mainClass;

    private static AppVersion version;

    @GetMapping("/version")
    public ResponseModel<AppVersion> version() {
        return ResponseModel.SUCCESS(version);
    }

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent event) {
        if(mainClass == null) {
            mainClass = event.getSpringApplication().getMainApplicationClass();
            version = new AppVersion(mainClass);
            logger.info("{} 系统的版本是：{}", version.getAppName(), version.getAppVersion());
        }
    }

}
